package io.renren.utils.wx.wxpay.impl;


import io.renren.utils.wx.wxpay.WXPayProperties;
import io.renren.utils.wx.wxpay.sdk.IWXPayDomain;
import io.renren.utils.wx.wxpay.sdk.WXPayConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

@Component
public class MyConfig extends WXPayConfig {

    @Autowired
    public WXPayProperties wxPayProperties;

    static Logger LOGGER = LoggerFactory.getLogger(MyConfig.class);


    /**
     * 域名
     */
    private String domain = "api.mch.weixin.qq.com";


    /**
     * 获取证书内容
     */
    private static InputStream certStream;

    /**
     * 获取 App ID
     *
     * @return App ID
     */
    @Override
    public String getAppID() {
        return wxPayProperties.getAppId();
    }


    /**
     * 获取 Mch ID
     *
     * @return Mch ID
     */
    @Override
    public String getMchID() {
        return wxPayProperties.getMchId();
    }


    /**
     * 获取 API 密钥
     *
     * @return API密钥
     */
    @Override
    public String getKey() {
        return wxPayProperties.getKey();
    }


    /**
     * 获取商户证书内容
     *
     * @return 商户证书内容
     */
    @Override
    public InputStream getCertStream() {
        try {
            certStream = new FileInputStream(new File(wxPayProperties.getCertPath()));
        } catch (FileNotFoundException e) {
            LOGGER.info("[获取微信支付证书流出错 {}]", e.getMessage());
            LOGGER.info(e.getMessage(), e);
        }
        return certStream;
    }

    public String getDomain() {
        return domain;
    }

    @Override
    public IWXPayDomain getWXPayDomain() {
        IWXPayDomain iwxPayDomain = new MyWXPayDomain();
        return iwxPayDomain;
    }


}
